new Vue({
    el: "#mainContent",
    data: {
        cardTitle: "",
        cardContent: "",
        firstMenuId: "",
        secondMenuId: "",
        firstMenuList: [],
        secondMenuList: [],
        getFirstMenuList: "/index/get_first_menu.ajax",
        getSecondMenuList: "/index/get_second_menu.ajax",
        saveCardUrl: "/card/add_card.ajax",
        cardIndexUrl: "/card/index"
    },
    mounted: function () {
        this.initPage();
    },
    methods: {
        initPage: function () {
            this.$http.get(this.getFirstMenuList)
                .then((response) => {
                    console.log(JSON.stringify(response.data.data));
                    this.$set(this, "firstMenuList", response.data.data);
                })
                .catch(function (response) {
                    console.log(response)
                });
        },
        initSelect: function () {
            var config = {
                '.chosen-select': {},
                '.chosen-select-deselect': {
                    allow_single_deselect: true
                },
                '.chosen-select-no-single': {
                    disable_search_threshold: 10
                },
                '.chosen-select-no-results': {
                    no_results_text: 'Oops, nothing found!'
                },
                '.chosen-select-width': {
                    width: "95%"
                }
            }
            for (var selector in config) {
                $(selector).chosen(config[selector]);
            }
        },
        chooseFirstMenu: function (event) {
            this.firstMenuId = event.target.value;
            //获取二级菜单
            this.$http.post(this.getSecondMenuList, {firstMenuId: this.firstMenuId}, {emulateJSON: true})
                .then((response) => {
                    console.log(JSON.stringify(response.data));
                    this.$set(this, "secondMenuList", response.data.data);
                })
                .catch(function (response) {
                    console.log(response)
                });
        },
        chooseSecondMenu: function (event) {
            this.secondMenuId = event.target.value;
        },
        submitCard: function () {
            this.$http.post(this.saveCardUrl, {
                cardTitle: this.cardTitle,
                cardContent: this.cardContent,
                typeId: this.secondMenuId,
            }, {emulateJSON: true})
                .then((response) => {
                    if (Number(response.data.code) == 0) {
                        alert("发帖成功!");
                        window.location.href = this.cardIndexUrl;
                    } else {
                        alert("发帖失败!");
                    }
                })
                .catch(function (response) {
                    console.log(response)
                });
        }
    }
});